<template>
  <el-form ref="form" :model="form" label-width="80px" :rules="rules">
    <el-form-item label="类型" prop="type">
      <el-radio-group v-model="form.type">
        <el-radio :label="1">表单</el-radio>
        <el-radio :label="2">问卷</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="表单名称" prop="name">
      <el-input v-model="form.name" />
    </el-form-item>
    <el-form-item label="表单描述">
      <el-input v-model="form.desc" type="textarea" :rows="4" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit('form')">提交</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import formApi from '@/api/form.js'
import log from '@/api/log'

export default {
  props: ['formdata'],

  data() {
    return {
      form: {
        id: '',
        name: '',
        desc: '',
        type: 1
      },
      rules: {
        name: [{ required: true, message: '请输入表单名称', trigger: 'blur' }],
        type: [{ required: true, message: '请选择表单类型', trigger: 'change' }]
      }
    }
  },
  watch: {
    formdata() {
      this.form = this.formdata
    },
    deep: true
  },
  created() {
    this.form = this.formdata
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate(async(valid) => {
        if (valid) {
          let res = ''
          if (this.form.id) {
            res = await formApi.edit(this.form)
          } else {
            res = await formApi.add(this.form)
          }

          if (res.code === 0) {
            this.$message.success(res.msg)
            this.$emit('success')
          } else {
            this.$message.error(res.msg)
          }
        }
      })
    }
  }
}
</script>
